/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getCollectionList,
  verifycatCollection,
  getPayBackList,
  getNoPayBackList,
  getPaymentDetail,
  getPaymentDownload,
} from '@/services/orderManagement';
import { message } from 'antd'
import moment from "moment/moment";
import { isEmpty, isArray } from 'lodash'

export default {
  namespace: "payment",

  state: {
    paymentList: {
      list: null,
      pagination: null,
    },
    payBackList: {
      list: null,
      pagination: null,
    },
    noPayBackList: {
      list: null,
      pagination: null,
    },
    paymentDetail: {},
    searchFormValue: {  //代收货款搜索表格数据
      data_start: moment().startOf("day").subtract(6, "days"),
      data_end: moment().endOf("day").subtract(0, "days"),
      siteId: 'all',
      customerSearch: '',
      collectionStatus: undefined,
      expressNumber: '',
      signStatus: undefined,
      salesmanSearch: "",
      pickupCode: "",
      addresseeSearch: ""
    }
  },

  effects: {
    *getCollectionPaymentList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getCollectionList, payload);

      if (!response) return;
      const { code, data: { data, count, page }, msg } = response;
      let newData = !isEmpty(data) ? data.map((item, index)=>{
        if( item.status == 1 ){
          item.disabled = false
        }else{
          item.disabled = true
        }
        return item
      }) : []

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            paymentList: {
              list: newData,
              pagination: {
                total: count,
                current: page,
                pageSize: 100,
                showQuickJumper: false,
                showSizeChanger: false,
              }
            }
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *verifycatCollection({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(verifycatCollection, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        message.success(msg)
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getPayBackList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPayBackList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const {  count, page } = data;
      let list = isArray(data.data) ? data.data : []

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            payBackList: {
              list,
              pagination: false
            }
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getNoPayBackList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getNoPayBackList, payload);

      if (!response) return;
      const { code, data, msg } = response;
      const { count, page } = data;
      let list = isArray(data.data) ? data.data : []

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            noPayBackList: {
              list,
              pagination: false
            }
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getPaymentDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPaymentDetail, payload);

      if (!response) return;
      const { code, data, msg } = response;  

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            paymentDetail: data ? data : {}
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.info("暂无数据！");
      }
    },
    *getPaymentDownload({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPaymentDownload, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        message.success(msg)
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};